module adv_1d_test_case_mod

  use adv_1d_common_mod

  implicit none

  real, parameter :: xmin = 0
  real, parameter :: xmax = 1
  real, parameter :: xrange = xmax - xmin
  real, allocatable :: x (:)       ! Cell center coordinates
  real, allocatable :: xi(:)       ! Cell interface coordinates
  real dx                          ! Cell interval
  real :: dt = 1.0                 ! Time step size
  integer :: nx = 100              ! Cell number
  integer :: nt = 200              ! Integration time step number
  real :: u = 0.005                ! Advection speed

contains

  subroutine adv_1d_test_case_init(case_name, ns, rho)

    character(*), intent(in) :: case_name
    integer, intent(in) :: ns
    real, intent(out) :: rho(1-ns:nx+ns)

    integer i

    allocate(x (1-ns:nx+ns))
    allocate(xi(1-ns:nx+ns))

    ! Set mesh grid coordinates.
    dx = xrange / nx
    do i = lbound(x, 1), ubound(x, 1)
      x(i) = (i - 1) * dx
    end do
    do i = lbound(xi, 1), ubound(xi, 1)
      xi(i) = x(i) + 0.5 * dx
    end do

    ! Set initial condition.
    select case (case_name)
    case ('square')
      do i = 1, nx
        if (x(i) >= 0.05 .and. x(i) <= 0.3) then
          rho(i) = 1.0d0
        else
          rho(i) = 0.0d0
        end if
      end do
    case ('sine')
      do i = 1, nx
        rho(i) = sin(2 * pi * x(i))
      end do
    case default
      write(*, *) '[Error]: Unknown test case name: ', case_name
      stop 1
    end select
    call apply_bc(ns, nx, rho)

  end subroutine adv_1d_test_case_init

  subroutine adv_1d_test_case_final()

    if (allocated(x )) deallocate(x )
    if (allocated(xi)) deallocate(xi)

  end subroutine adv_1d_test_case_final

end module adv_1d_test_case_mod